<?php 
namespace app\admin\validate;
use app\admin\code\AdminCode;
use app\admin\code\AuthCode;
use app\admin\code\BaseCode;
use app\admin\model\BaseModel;
use think\Validate;

class AuthValidate extends BaseValidate
{
    protected $_rule = [
        'id'      =>  'require|check_child_exist',
        'name'    =>  'require',
        'module'  =>  'require',
        'is_menu' =>  'in:0,1',
        'sort'    =>  'number'
    ];
    protected $_field = [
        'id'=>'权限',
        'name'=>'权限名字',
        'module'=>'模块',
        'is_menu'=>'是否菜单',
        'sort'=>'排序',
    ];
    protected $_message = [
        'id.check_child_exist' =>AuthCode::HAVE_CHILD_CANT_DEL,
        'id.require' => BaseCode::PARAM_CANT_EMPTY,
        'name.require' => BaseCode::PARAM_CANT_EMPTY,
        'module.require' => BaseCode::PARAM_CANT_EMPTY,
        'is_menu.in' => BaseCode::PARAM_MUST_IN,
        'sort.number' =>BaseCode::PARAM_MUST_NUMBER,
    ];
    protected $_scene = [
        'del'    => ['id'],
        'update' => ['id'=>'require','name','module','is_menu','sort'],
        'add'    => ['name','module','is_menu','sort'],
    ];
    protected $_extend_rule = ['check_child_exist'];


    public  function check_child_exist(AuthValidate $obj,$data){
      if($obj->_model->check_child_exist($data)){
          return false;
      }else{
          return true;
      }
    }
}


